﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class HC360 : Information
    {
        string name = "", uid = "", visitid = "";
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("LoginID", name);
            pd.Add("Passwd", pass);
            pd.Add("submit", "请登录");
            string html = source.Post("http://sso.hc360.com/ssologin?ReturnURL=http://my.b2b.hc360.com/my/turbine/template/corcenter,company,main.html&renew=true", pd.Read(), "http://www.hc360.com/");
            if (html.IndexOf("hcmy_homepagelog=Quit')\">退出</a>]") != -1)
            {
                this.name = name;
                uid = CookieFactory.GetInstance().GetCookie("hc360.com", "hc360_userid");
                visitid = CookieFactory.GetInstance().GetCookie("hc360.com", "hc360visitid");
                return true;
            }
            return false;
        }
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            title = tek.Get(title);
            //提交
            InformationResult result = new InformationResult();
            string html = source.Get("http://my.b2b.hc360.com/my/turbine/template/corcenter%2Cbusiness%2Cbusindetailedit.html?tag=1");
            Upload(Regex.Match(html, "picstr=([^\"]+)\">").Groups[1].Value);
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("isMmtShopUser", "");
            pd.Add("username", this.name);
            pd.Add("l3supcatid", "");
            pd.Add("bc_id", "");
            pd.Add("rebc_id", "");
            pd.Add("bcid", "");
            pd.Add("rebcid", "");
            pd.Add("isshowl3supcat", "0");
            pd.Add("areaId", Regex.Match(html, "name=\"areaId\"  value=\"([0-9]+)\"").Groups[1].Value);
            pd.Add("providerid", Regex.Match(html, "\"providerid\" value=\"([0-9]+)\"").Groups[1].Value);
            pd.Add("loginid", "");
            pd.Add("regid", "");
            pd.Add("sorttag", "0");
            pd.Add("sessionid", Regex.Match(html, "name=\"sessionid\" size=\"10\" value=\"([0-9]+)\"").Groups[1].Value);
            pd.Add("trustvalue", "0");
            pd.Add("supcatid", site.TypeId[1]);
            pd.Add("catid", site.TypeId[2]);
            pd.Add("isselkeyword", "0");
            pd.Add("selsupcatid", "0");
            pd.Add("opertype", "0");
            pd.Add("viewid", "");
            pd.Add("preview360path", "");
            pd.Add("view360path", "");
            pd.Add("view360note", "");
            pd.Add("unit", pif.Get("单位"));
            pd.Add("pricerange", pif.Get("价格"));
            pd.Add("num", pif.Get("库存数量"));
            pd.Add("minordernum", "1");
            pd.Add("timelimit", "2");
            pd.Add("complete", "10");
            pd.Add("matchSupcatid", "");
            pd.Add("validdate", "180");
            pd.Add("ispreviewdb", "0");
            pd.Add("contacter", "");
            pd.Add("telephone", pif.Get("手机"));
            pd.Add("fax", pif.Get("电话"));
            pd.Add("name", "");
            pd.Add("mp", "");
            pd.Add("email", pif.Get("EMAIL"));
            pd.Add("iswebuser", "");
            pd.Add("fu", "0");
            pd.Add("supplytag", "0");
            pd.Add("keyword", pif.Get("产品名称"));
            pd.Add("last10supcat", site.TypeId[1]);
            pd.Add("catIdr", "");
            pd.Add("l3supercat", site.TypeId[0]);
            pd.Add("secondsupcatid", site.TypeId[1]);
            pd.Add("tradecatcount", "0");
            pd.Add("bsid", "0");
            pd.Add("seriesName", "");
            pd.Add("brandname", pif.Get("产品名称"));
            pd.Add("type", pif.Get("产品名称"));
            pd.Add("attcount", "0");
            pd.Add("title", title);
            pd.Add("title2", title);
            pd.Add("introduce", content);
            pd.Add("saveTag223", "saveTagValue223");
            html = source.Post("http://my.b2b.hc360.com/my/turbine/action/business.BusinAction/eventsubmit_doeditsave/doEditsave", pd.Read(), "	http://my.b2b.hc360.com/my/turbine/template/corcenter%2Cbusiness%2Cbusindetailedit.html");
            if (html.IndexOf("恭喜您!商机发布成功") != -1)
            {
                html = source.Get("http://my.b2b.hc360.com/my/turbine/template/corcenter%2Cbusiness%2Cbusinuncheck.html?querysorttag=0");
                result.Code = 1;
                result.Content = "http://b2b.hc360.com/supplyself/" + Regex.Match(html, "businPreview\\(([0-9]+),[^>]+>\\s*" + title).Groups[1].Value + ".html";
            }
            else if (Regex.IsMatch(html, "language=javascript><!--	alert \\(\"([^\"]+?)\"\\);"))
            {
                result.Content = Regex.Match(html, "language=javascript><!--	alert \\(\"([^\"]+?)\"\\);").Groups[1].Value;
            }
            return result;
        }
        public string Upload(string picstr)
        {
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("picstr", picstr);
            //发送文件
            sfd.WriteFile("upFile", RandomPicture.Get());
            //尾部数据
            //结尾
            string html = source.Post("http://imgup.b2b.hc360.com/imgup/turbine/action/imgup.SwfUploadFileAction/eventsubmit_doupload/doUpload?picindex=1&isAddWater=1&q=1", sfd.GetContentType(), sfd.Read(), "http://style.org.hc360.com/js/build/module/imgup/hc360upload.swf");
            return Regex.Match(html, "'url':'([^']+)'").Groups[1].Value;
        }
    }
}
